#ifndef Q3BSP_H_INCLUDED
#define Q3BSP_H_INCLUDED

#include <sheeplib/math/vec2.h>
#include <sheeplib/math/vec3.h>
#include "map/worldmesh.h"
#include "resources/texturemanager.h"

namespace engine {
		
	namespace map {
		
		class WorldMesh;
		
		enum enQ3LumpOffsets {
		   iEntitiesOffset = 0,
		   iTextureOffset,
		   iPlanesOffset,
		   iNodesOffset,
		   iLeafOffset,
		   iLeafFacesOffset,
		   iLeafBrushesOffset,
		   iModelsOffset,
		   iBrushesOffset,
		   iBrushesSidesOffset,
		   iVerticesOffset,
		   iMeshVerticesOffset,
		   iShaderFilesOffset,
		   iFacesOffset,
		   iLightMapsOffset,
		   iLightVolumeOffset,
		   iVisibleDataOffset,
		   iMaxLumpsOffset
		};

		struct stQ3Header {
		   char bspID[4];
		   int version;
		};

		struct stQ3FileLump {
		   int offset;
		   int length;
		};

		struct stQ3Texture {
		   char file[64];
		   int flags;
		   int contents;
		};

		struct stQ3Face {
		   int texID;
		   int effect;
		   int type;
		   int startVertexIndex;
		   int totalVertices;
		   int meshVertexIndex;
		   int totalMeshVertices;
		   int lightmapID;
		   int lightMapCorner[2];
		   int lightMapSize[2];
		   slVec3 lightMapPos;
		   slVec3 lightMapVectors[2];
		   slVec3 normal;
		   int size[2];
		};

		struct stQ3Vertex {
		   slVec3 vertex;
		   slVec2 texCoord;
		   slVec2 lightmapCoord;
		   slVec3 normal;
		   unsigned char color[4];
		};

		struct stQ3LightMap {
		   unsigned char lightMap[128][128][3];
		};

		class Q3BSP : public WorldMesh {
			private:
			
				void storeBSPData(const vector<stQ3Vertex>& vertices,
								  const vector<stQ3Face>& faces,
								  const vector<stQ3Texture>& textures,
								  const vector<stQ3LightMap>& lightmaps);
								  
				
			public:
				Q3BSP(const string& name);
				~Q3BSP();
				
				bool loadMap(const string& filename);
				void unloadMap();
				
				static shared_ptr<Q3BSP> create(const string& name) {
					return shared_ptr<Q3BSP>(new Q3BSP(name));
				}
		};
	}
}

#endif // Q3BSP_H_INCLUDED
